namespace Angelus.Oracle.Entity.Common.Entity
{
    using System;
    using System.Globalization;
    using System.IO;
    using System.Text;

    /// <summary>
    /// Description of SqlWriter.
    /// </summary>
    internal class SqlWriter : StringWriter
    {
        #region Fields

        private int _indent;
        private bool _proceed;

        #endregion

        #region Constructors

        /// <summary>
        /// 
        /// </summary>
        /// <param name="builder"></param>
        public SqlWriter(StringBuilder builder)
            : base(builder, CultureInfo.InvariantCulture)
        {
            this._indent = -1;
            this._proceed = true;
        }

        #endregion

        #region Properties

        /// <summary>
        /// The number of tabs to be added at the beginning of each new line
        /// </summary>
        internal int Indent
        {
            get { return this._indent; }
            set { this._indent = value; }
        }

        #endregion

        #region StringBuilder Overrides

        /// <summary>
        /// Reset proceed if we detect the newline string.
        /// </summary>
        /// <param name="value"></param>
        public override void Write(string value)
        {
            if (Environment.NewLine == value)
            {
                base.WriteLine();
                this._proceed = true;
            }
            else
            {
                if (this._proceed)
                {
                    if (this._indent > 0)
                    {
                        base.Write(new string('\t', this._indent));
                    }

                    this._proceed = false;
                }
                base.Write(value);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public override void WriteLine()
        {
            base.WriteLine();
            this._proceed = true;
        }

        #endregion
    }
}